package tool;

import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class ImageUtil {

	public static Image createImage(String file, boolean archived)
	{
		
		Image img = null;
		byte[]data ;
		int buffSize=0;
		try {
			if(archived)
			{
				InputStream is;
				is = new Object().getClass().getResourceAsStream(file);
				buffSize = is.available();
				data = new byte[buffSize];
				for(int i = 0; i < data.length;i++)
					data[i]=(byte)is.read();
				img = Toolkit.getDefaultToolkit().createImage(data);
				is.close();
				is = null;
			}else {
				File imageFile = new File(file);
				if(!imageFile.exists()){
					return null;
				}
				FileInputStream fis = new FileInputStream(imageFile);
				buffSize = fis.available();
				data = new byte[buffSize];
				fis.read(data);
				img = Toolkit.getDefaultToolkit().createImage(data);
				fis.close();
				fis = null;
			}
		
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			data = null;
		}
		return img;
	}
}
